import React, { Component } from 'react';
import { List, InputItem, Button, Flex, WhiteSpace, Toast } from 'antd-mobile';
import { createForm } from 'rc-form';
import { Link } from 'react-router-dom';

import '../css/common.css';
import '../css/login.css';

class companyLogin extends Component {
  constructor(props){
    super(props);
  }

  pwLogin=(e)=>{
    this.props.form.validateFields((error, value) => {
      if( error ){
        Toast.info('请填写完整的信息 !!!', 2);
        return;
      }else{
        // submit
        value.account = value.account.replace(/\s+/g, "");
        let _url = `http://58.220.10.23:8001/api/user/login?account=${value.account}&password=${value.password}`;
        fetch(_url, { method: 'POST', credentials: 'include',
    	   }).then(response => response.json())
    	   .then(data=>{
            //console.log(data);
            if( data.status==0 ){
              Toast.success('登录成功！', 2);
              this.props.history.push('companyClient');
            }else{
              Toast.fail(data.errorMsg, 2);
            }
    	   })
      }
    });
  }

  render() {
    const { getFieldProps } = this.props.form;
    return (
      <div className="skin-orange">
        <List>
          <Flex>
            <p className="title">公司登录</p>
          </Flex>
          <InputItem
            {...getFieldProps('account',{ rules: [{required:true}] })}
            type="phone"
            placeholder="请输入手机号"
            clear
            moneyKeyboardAlign="left"
          ></InputItem>
          <InputItem
            {...getFieldProps('password',{ rules: [{required:true}] })}
            type="password"
            placeholder="请输入密码"
            clear
          ></InputItem>
          <List.Item>
            <Button type="primary" onClick={this.pwLogin} className="orange-bg orange-border">登录</Button>
          </List.Item>
          <List.Item>
            <Link to="/companyLoginIden" id="togglefor-pw" className="btn-toggle">切换到手机验证码登录</Link>
          </List.Item>
        </List>
      </div>

    );
  }
}

const _companyLogin = createForm()(companyLogin);
export default _companyLogin;
